package diona.snippet

import net.liftweb.util.BindHelpers._
import net.liftweb.http.{SHtml, StatefulSnippet}
import net.liftweb.util.CssSel
import diona.emotions.PolarityClassifier
import diona.model.{FacebookSearch, TwitterSearch}
import diona.search.PostInfo

/**
 * User: anton
 * Date: 19.06.11 14:24
 */
class Sentiments extends StatefulSnippet
{
  private var query = ""

  def dispatch =
  {case "render" => render}

  def fillItems(posts: List[PostInfo]): CssSel =
  {
    "*" #> posts.map(t => {
      ".postItem [name]" #> PolarityClassifier.classify(t.snippet.getOrElse("")) &
        ".postItemText *" #> t.snippet.getOrElse("")
    })
  }

  def render: CssSel =
  {
    "name=query" #> SHtml.text(query, query = _, "id" -> "the_query") &
      "#twitterPosts *" #> fillItems(TwitterSearch.search(query)) &
      "#facebookPosts *" #> fillItems(FacebookSearch.search(query))

  }
}